import {Injectable} from '@angular/core';

export interface ParentCategory {
  value?: string;
  label?: string;
  id: number;
  name: string;
  code: string;
  parentName: string;
  parentId: number;
  isLeaf?: boolean;
  allParentId?: string;
  allParentName?: string;
  children?: Array<ParentCategory>;
}


@Injectable({
  providedIn: 'root'
})
export class Util {
  /*处理接口返回的类别数据，返回可供nz-cascade组件可用的数据格式*/
  public getCascaderData(array: Array<ParentCategory>): Array<ParentCategory> {
    array.forEach(item => {
      item.value = item.id.toString();
      item.label = item.name;
      item.isLeaf = !item.children.length;
      if (item.children) {
        this.getCascaderData(item.children);
      }
    });
    return array;
  }

  public getCascaderDataY(object: ParentCategory): Array<any> {
    const {allParentId, allParentName} = object;
    const idArray = allParentId && allParentId.split(',');
    const nameArray = allParentId && allParentName.split(',');
    const newArray = [];
    if (idArray && idArray.length) {
      for (let i = 0; i < idArray.length; i++) {
        newArray.push({
          value: idArray[i],
          label: nameArray[i]
        });
      }
    }
    return newArray;

  }
}
